package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

@Controller
public class LoginController {

    @Autowired
    private UserService userService;

    // 跳转到登录页
    @GetMapping("/login")
    public String loginPage() {
        return "login";
    }

    // 处理登录请求
    @PostMapping("/doLogin")
    public String doLogin(
            @RequestParam String username,
            @RequestParam String password) {
        User user = userService.login(username, password);
        if (user != null) {
            return "redirect:/success";
        }
        return "redirect:/login?error";
    }

    @GetMapping("/success")
    public String success() {
        return "success";
    }

    // 跳转到注册页
    @GetMapping("/register")
    public String registerPage() {
        return "register";
    }

    // 处理注册请求
    @PostMapping("/doRegister")
    public String doRegister(
            @RequestParam String username,
            @RequestParam String password,
            Model model) {
        if (!isValidPassword(password)) {
            model.addAttribute("error", "密码必须包含英文字母、数字和特殊字符！");
            return "register";
        }
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean result = userService.register(user);
        if (result) {
            return "redirect:/register?success";
        }
        return "redirect:/register?error";
    }

    // 信息管理页面 - 显示所有用户
    @GetMapping("/user/manage")
    public String manageUsers(Model model) {
        List<User> users = userService.findAll();
        model.addAttribute("users", users);
        return "user_manage";
    }

    // 编辑用户页面
    @GetMapping("/user/edit/{id}")
    public String editUser(@PathVariable Long id, Model model) {
        User user = userService.findById(id);
        model.addAttribute("user", user);
        return "user_edit";
    }

    // 更新用户信息
    @PostMapping("/user/update")
    public String updateUser(@ModelAttribute User user) {
        userService.update(user);
        return "redirect:/user/manage";
    }

    // 删除用户
    @GetMapping("/user/delete/{id}")
    public String deleteUser(@PathVariable Long id) {
        userService.delete(id);
        return "redirect:/user/manage";
    }

    // 跳转到修改密码页面
    @GetMapping("/changePassword")
    public String changePasswordPage() {
        return "change_password";
    }

    // 处理修改密码请求
    @PostMapping("/doChangePassword")
    public String doChangePassword(
            @RequestParam String username,
            @RequestParam String oldPassword,
            @RequestParam String newPassword,
            Model model) {
        if (!isValidPassword(newPassword)) {
            model.addAttribute("error", "密码必须包含英文字母、数字和特殊字符！");
            return "change_password";
        }
        // 验证旧密码是否正确
        User user = userService.login(username, oldPassword);
        if (user == null) {
            model.addAttribute("error", "旧密码不正确！");
            return "change_password";
        }

        // 更新密码
        user.setPassword(newPassword);
        userService.updatePassword(user);

        model.addAttribute("success", "密码修改成功！");
        return "change_password";
    }

    // 跳转到查询用户页面
    @GetMapping("/queryUser")
    public String queryUserPage() {
        return "user_query";
    }

    // 处理查询用户请求
    @GetMapping("/doQueryUser")
    public String doQueryUser(@RequestParam String username, Model model) {
        List<User> users = userService.findByUsernameLike(username);
        model.addAttribute("users", users);
        return "user_query";
    }

    private boolean isValidPassword(String password) {
        String regex = "^(?=.*[A-Za-z])(?=.*\\d)(?=.*[@$!%*#?&])[A-Za-z\\d@$!%*#?&]+$";
        return Pattern.matches(regex, password);
    }
}